<?php
namespace app\admin\controller;
use app\model\Exam as ExamModel;
use think\Db;


class Exam extends Base
{
    public function __construct(){
        $model = new ExamModel();
        $this->model = $model;
        parent::__construct();
        
        $page_info = $this->page_info;
        $page_info['title'] = '焦点图';

        $this->assign([
            'model'=>$this->model,
            'page_info'=>$page_info
        ]);
    }

    public function assignFields(){
        $model = $this->model;

        $fields = [
            'name'=>['name'=>'名称','type'=>'text','valids'=>['required']],
            'image'=>['name'=>'图片','type'=>'file'],
            'status'=>['name'=>'状态','type'=>'select1','params'=>$model->getStatusList()],
            'sort'=>['name'=>'排序值','type'=>'text'],
            'description'=>['name'=>'描述','type'=>'textarea'],
        ];
        $valids = createPageValid($fields);
        $this->assign([
            'fields'=>$fields,
            'valids'=>$valids
        ]);

    }

    public function assignSearchs(){
        $model = $this->model;
        $search_params = [
            [
                ['key'=>'ctime','name'=>'日期','type'=>'date'],
            ]
        ];
        $this->assign([
            'search_params'=>$search_params
        ]);
    }

    public function index(){
        $model = $this->model;
        $map_params = [
            ['key'=>'ctime','type'=>'='],
        ];
        $map = getSearchParam($map_params);

        $page_param = [
            'page'=>input('page',1),
            'list_rows'=>input('page_size',20)
        ];
        // 显示筛选
        $this->assignSearchs();

        $list = $model->where($map)->order('id','desc')->paginate($page_param);
        $page = $list->render();
        $list = $list->toarray();
        foreach ($list['data'] as $k => $vo) {
            $list['data'][$k]['user'] = Db::name('user')->where('id',$vo['user_id'])->find();
            $total_num = $vo['total_snum'] + $vo['total_enum'];
            $list['data'][$k]['total_num'] = $total_num;
            $list['data'][$k]['success_radio'] = (round($vo['total_snum'] / $total_num,2)*100).'%';
        }
        return view('index', [
            'list'=>$list,
            'page'=>$page
        ]);
    }

   

   
}
